export const isWPF = true;
const REQUEST_URL = "http://10.161.0.53:31900/aiplatform";
// const REQUEST_URL = "http://192.168.1.62:61100/aiplatform";
// const REQUEST_URL = "http://10.21.31.33:32648/aiplatform";
const multipartAry = [
    `material-info/upload`,
    `material-info/save-and-upload`,
    `/platform/predict-task/upload`,
    `/platform/predict-task/ocr-single`,
    `/analyze/onecommet`
    // `/import-model-task/upload/${params.id}`,
    // `/import-model-task/${params.id}/import-model-predict`,
    // `/calibration/upload/${params.id}`,
    // `/material-info/upload-no-project/${id}`
];
const regexAry = [
    /^\/import-model-task\/(\w+)\/import-model-predict$/,
    /^\/import-model-task\/upload\/(\w+)$/,
    /^\/calibration\/upload\/(\w+)$/,
    /^\/material-info\/upload-no-project\/(\w+)$/
];

// const isMatchedByRules = function(str, rules) {
//     // 遍历正则表达式数组
//     for (let i = 0; i < rules.length; i++) {
//         // 使用RegExp对象的test方法来测试字符串是否符合当前正则表达式
//         if (rules[i].test(str)) {
//             // 如果找到一个匹配的规则，返回true
//             return true;
//         }
//     }

//     // 如果没有找到匹配的规则，返回false
//     return false;
// };
// const RequestHeaders_Content_Type = function(config) {
//     const url = config.url;
//     if (multipartAry.includes(url) || isMatchedByRules(url, regexAry)) {
//         return "multipart/form-data"; // ;charset=UTF-8
//     } else {
//         return "application/json"; // ;charset=UTF-8
//     }
//     // application/x-www-form-urlencoded
// };

const getQueryParams = function (params, isStringParam) {
    if (isStringParam) {
        const url = new URLSearchParams(params);
        let result = {};
        for (const pair of url.entries()) {
            result[pair[0]] = pair[1];
        }
        return result;
    } else {
        return params;
    }
};
// const getUrlBeforeQueryParams = function(url) {
//     const urlParts = url.split("?");
//     return urlParts[0];
// };
// const getUrlAfterQueryParams = function(url) {
//     const urlParts = url.split("?");
//     return urlParts[1];
// };
export const generateRandomUUID = function () {
    const hexList = [];
    for (let i = 0; i <= 15; i++) {
        hexList[i] = i.toString(16);
    }
    let uuid = "";
    for (let i = 1; i <= 36; i++) {
        if (i === 9 || i === 14 || i === 19 || i === 24) {
            uuid += "-";
        } else if (i === 15) {
            uuid += 4;
        } else if (i === 20) {
            uuid += hexList[(Math.random() * 4) | 8];
        } else {
            uuid += hexList[(Math.random() * 16) | 0];
        }
    }
    // return uuid.replace(/-/g, "");
    return uuid;
};
export const convertIdsToQueryString = function (ids) {
    let result = "";
    for (let i = 0; i < ids.length; i++) {
        result += "ids=" + ids[i];
        if (i < ids.length - 1) {
            result += "&";
        }
    }
    return "?" + result;
};
export const convertIdListToQueryString = function (ids) {
    let result = "";
    for (let i = 0; i < ids.length; i++) {
        result += "idList=" + ids[i];
        if (i < ids.length - 1) {
            result += "&";
        }
    }
    return "?" + result;
};
export const sendWebView2 = async (
    url,
    METHOD,
    HEADERS_CONTENT_TYPE,
    params,
    isStringParam,
    uuid
) => {
    await window.chrome.webview.postMessage({
        RequestUrl: REQUEST_URL + url,
        RequestMethod: METHOD,
        Headers: {
            contentType: HEADERS_CONTENT_TYPE
        },
        // QueryParameter: getQueryParams(requestBodyUrl),
        RequestBody: getQueryParams(params, isStringParam),
        WebInfo: { requestId: uuid }
    });
};
